<?php
namespace Home\Controller;

use Think\Controller;
class UserController extends BaseController{

    function httpGet($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }


    public function get_openid(){

        if(!$_POST['code']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:code';
            $this->ajaxReturn($return_data);
        }

        $appid = "wx4d4fe22efa535c1d";
        $appsecret = "9c703cf71480c3b476c7cba412e8e528";
        $grant_type = "authorization_code"; //授权
        $code = $_POST['code'];    //有效期5分钟 登录会话

        // 拼接url
        $url = "https://api.weixin.qq.com/sns/jscode2session?"."appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=".$grant_type;
        $res = json_decode($this->httpGet($url),true);   //将json解码成数组
        $openid = $res['openid'];  //取出json里对应的值

        if($openid){
            $return_data['error_code'] = 0;
            $return_data['msg'] = '获取成功';
            $return_data['openid'] = $openid;
            $this->ajaxReturn($return_data);
        }else{
            $return_data['error_code'] = 2;
            $return_data['msg'] = '获取失败';
            $this->ajaxReturn($return_data);
        }
    }


	public function login(){
        /*
           用户注册，一定要记得前端和后端是分开的，所以前端检查过也要检查
        */
        if(!$_POST['user_openid']){
            $return_data['error_code'] = 1;
            $return_data['msg'] = '参数不足:user_openid';
            $this->ajaxReturn($return_data);
        }
        //检查是否已有该用户

        $userlist = M('user_list');

        //构造查询条件
        $where = array();
        $where['user_openid']=$_POST['user_openid'];


        $result=$userlist->where($where)->find();

        //已授权登录过
        if($result){

            $return_data['error_code']=0;
            $return_data['msg']='登录成功';
            $return_data['data']= $result;
            $this->ajaxReturn($return_data);
        }else{
            //未授权登录
            //校验参数是否存在
            $key = array('user_openid','user_name','user_school','user_faculty','user_picurl');
            $return_data = array();

            foreach ($key as $dest => $value) {

                if(!$_POST[''.$value]){
                    $return_data['error_code'] = 1;
                    $return_data['msg'] = '参数不足:'.$value;
                    $this->ajaxReturn($return_data);
                }
            }
            //构建插入的数据
            $data =array();
            $data = $_POST;
            $data['gold_all_num'] = 10;

            //插入数据
            $result = $userlist->add($data);//add函数添加数据成功之后，返回该条数据id

            if($result){

                $return_data['error_code'] = 0;
                $return_data['msg'] = '登录成功';
                $return_data['data'] = $data;
                $return_data['data']['user_id'] = $result;

                $this->ajaxReturn($return_data);

            }else{
                //插入数据失败

                $return_data['error_code'] = 2;
                $return_data['msg'] = '注册失败';
                $this->ajaxReturn($return_data);
            }
        }
    }


		//改变学校或学院或用户名或头像
	public function change(){

			$key = array('user_openid','user_name','user_school','user_faculty','user_picurl');
	        $return_data = array();
	        $where = array();
	        $where['user_openid'] = $_POST['user_openid'];
	        $data = array();
	        $userlist = M('user_list');

	       foreach ($key as $dest => $value) {
	           if(!$_POST[''.$value]){
		            $return_data['error_code'] = 1;
		            $return_data['msg'] = '参数不足:'.$value;
		            $this->ajaxReturn($return_data);
	           }

	           if($value == 'user_openid'){
	           	$result = $userlist->where($where)->find();
	           	if(!$result){
		           	$return_data['error_code'] = 2;
		            $return_data['msg'] = '该用户不存在';
		            $this->ajaxReturn($return_data);
	           	}
	           }
	           else{
	           		$data[''.$value] = $_POST[''.$value];

	           }
	       }
	       $result2 = $userlist->where($where)->save($data);
           $result = $userlist->where($where)->find();
	       if($result2){
	       			$return_data['error_code'] = 0;
		            $return_data['msg'] = '修改成功';
		            $return_data['data'] = $result;
		            $return_data['data']['user_id'] = $result['user_id'];
		            $this->ajaxReturn($return_data);
	       }else{
	       			$return_data['error_code'] = 3;
		            $return_data['msg'] = '修改失败';
		            $this->ajaxReturn($return_data);
	       }
	}

}